<?php 

namespace Cli\Event;
use Think\Log;

require_once(LIB_PATH.'/Com/QQ/XingeApp.class.php');
// +----------------------------------------------------------------------
// | 文件 PushEvent.class.php
// +----------------------------------------------------------------------
// | 说明 信鸽推送
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class PushTaskEvent extends TaskEvent
{

    static $Pusher;

    protected $accounts = [];
    protected $all = false;
    protected $tags = [];
    protected $tagop;
    protected $custom;
    protected $test = false;
    protected $messageType;

    public function run(&$param)
    {
        $type = !empty($param['type']) ? $param['type'] : '';
        if (!$type){
            return;
        }

        $accounts = !empty($param['accounts']) ? $param['accounts'] : [];
        $accounts = is_array($accounts) ? $accounts : [$accounts];
        $all      = !empty($param['toall']) ? $param['toall'] : 0;  // 全部推送
        $tags     = !empty($param['tags']) ? $param['tags'] : [];
        $tagop    = !empty($param['tagop']) ? $param['tagop'] : 'OR';
        $messageType     = !empty($param['messageType']) && $param['messageType'] == 'message' ? \Message::TYPE_MESSAGE : \Message::TYPE_NOTIFICATION;
        if (!$accounts && !$all && !$tags){
            // 如果没有传对象, 并且不是群发 不是按照tag 就不处理
            return;
        }
        $custom = !empty($param['custom']) ? $param['custom'] : '';
        $test   = !empty($param['test']) ? $param['test'] : 0;
        $plat   = !empty($param['plat']) ? strtolower($param['plat']) : '';

        $allPlats = ['android','ios'];
        if ($plat && in_array($plat, $allPlats)){
            $doPushPlat = [$plat];
        }else{
            $doPushPlat = $allPlats;
        }

        $this->accounts = $accounts;
        $this->all      = $all;
        $this->tags     = $tags;
        $this->tagop    = $tagop;
        $this->test     = $test;
        $this->custom   = $custom;
        $this->messageType   = $messageType;

        foreach ($doPushPlat as $platform) {
            switch ($platform) {
                case 'android':
                    $this->doPushAndroid();
                    break;
                case 'ios':
                    $this->doPushIOS();
                    break;
            }
        }
    }

    protected function doPushAndroid()
    {
        $conf = $this->test ? C('PUSH_ANDROID_DEV') : C('PUSH_ANDROID');
        $ak = $conf['ACCESS_ID'];
        $fk = $conf['SECRET_KEY'];
        if (!$ak){
            $log = "[swoole] - [doPushAndroid]: CONFIG ERR";
            echo $log;
            Log::record($log, 'DEBUG');
            Log::save();
            return;
        }
        if(empty(self::$Pusher['android'])){
            self::$Pusher['android'] = new \XingeApp($ak, $fk);
        }
        $AndrPusher = self::$Pusher['android'];
        $title      = !empty($this->custom['title'])    ? $this->custom['title'] : '66PLAY';
        $content    = !empty($this->custom['content'])  ? $this->custom['content'] : '';
        $activity   = !empty($this->custom['activity']) ? $this->custom['activity'] : '';
        
        $mess = new \Message();
        $mess->setTitle($title);
        $mess->setContent($content);
        $mess->setType($this->messageType);
        $mess->setStyle(new \Style(0, 1, 1));
        $action = new \ClickAction();
        $action->setActionType(\ClickAction::TYPE_ACTIVITY);
        $action->setActivity($activity);
        $mess->setAction($action);
        $mess->setCustom($this->custom);
        // print_r($mess);
        
        if ($this->tags){
            $ret = $AndrPusher->PushTags(0, $this->tags, $this->tagop, $mess);
        }
        else{
            $ret = $this->all ? $AndrPusher->PushAllDevices(0, $mess) : ( count($this->accounts) > 1 ? $AndrPusher->PushAccountList(0, $this->accounts, $mess) : $AndrPusher->PushSingleAccount(0, $this->accounts[0], $mess) );
        }
        $to = implode(',', $this->accounts);
        // print_r($ret);
        if ($ret['ret_code'] == 0){
            $log = "[swoole] - [doPushAndroid] - [success]: push to [{$to}] success";
        }else{
            $err = $ret['err_msg'];
            $log = "[swoole] - [doPushAndroid] - [error]: push to [{$to}] err: {$err}";
        }
        echo $log;
        Log::record($log, 'DEBUG');
        Log::save();
    }

    protected function doPushIOS()
    {
        $conf = $this->test ? C('PUSH_IOS_DEV') : C('PUSH_IOS');
        $ak = $conf['ACCESS_ID'];
        $fk = $conf['SECRET_KEY'];
        if (!$ak){
            $log = "[swoole] - [doPushIOS]: CONFIG ERR";
            echo $log;
            Log::record($log, 'DEBUG');
            Log::save();
            return;
        }
        if(empty(self::$Pusher['ios'])){
            self::$Pusher['ios'] = new \XingeApp($ak, $fk);
        }
        $IOSPusher = self::$Pusher['ios'];
        $content    = !empty($this->custom['content'])  ? $this->custom['content'] : '';
        $messios = new \MessageIOS();
        $messios->setExpireTime(0);
        if ($this->messageType == \Message::TYPE_NOTIFICATION){
            $messios->setAlert($content);
            $messios->setBadge(1);
            $messios->setSound("");
        }else{
            $messios->setAvailable(true);
            $messios->setBadge(0);
        }
       
        $messios->setCustom($this->custom);
        $env = $this->test ? \XingeApp::IOSENV_DEV : \XingeApp::IOSENV_PROD;
        if ($this->tags){
            $ret = $IOSPusher->PushTags(0, $this->tags, $this->tagop, $messios, $env);
        }
        else{
            $ret = $this->all ? $IOSPusher->PushAllDevices(0, $messios, $env) : ( count($this->accounts) > 1 ? $IOSPusher->PushAccountList(0, $this->accounts, $messios, $env) : $IOSPusher->PushSingleAccount(0, $this->accounts[0], $messios, $env) );
        }
        $to = implode(',', $this->accounts);
        // print_r($ret);
        if ($ret['ret_code'] == 0){
            $log = "[swoole] - [doPushIOS] - [success]: push to [{$to}] success";
        }else{
            $err = $ret['err_msg'];
            $log = "[swoole] - [doPushIOS] - [error]: push to [{$to}] err: {$err}";
        }
        echo $log;
        Log::record($log, 'DEBUG');
        Log::save();
    }

}